/**
 * 账户设置页面
 */
import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/entity/user_center/address_list_entity.dart';
import 'package:cbec_app/pages/my/controller/address_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:left_scroll_actions/left_scroll_actions.dart';

class MyAddress extends StatelessWidget {
  final AddressController addressCtr = Get.find<AddressController>();


  MyAddress({super.key});

  @override
  Widget build(BuildContext context) {
    return BasePage(
      title: "我的地址",
      iconType: IconType.back,
      children: [
        Container(
          width: double.infinity,
          height: 540.h,
          margin: EdgeInsets.fromLTRB(8.w, 10.w, 8.w, 10.w),
          child: Obx(() => Container(
            decoration: border_radis(Colors.white),
            child: addressCtr.addressList!.isNotEmpty? Container(
              padding: EdgeInsets.all(10.w),
              child: Tools.getPullRefresh(
                  refreshController: addressCtr.baseRefreshCtr,
                  onRefresh: addressCtr.onPageRefresh,
                  onLoading: addressCtr.onPageLoading,
                  child: ListView.builder(
                      itemCount: addressCtr.addressList!.length,
                      itemBuilder: (context, index) {
                        final item = addressCtr.addressList?[index];
                        return _addressItems(item: item,index:index);
                      })),
            ): const NotFound(txt: "暂无收货地址,请新建收货地址",notfoundType: NotFoundType.address),
          )),
        ),
        //新增地址按钮
        Positioned(
            key: GlobalKey(debugLabel: 'Positioned'),
            bottom: MediaQuery.of(context).padding.bottom + 10.w,
            child: Container(
              padding: EdgeInsets.fromLTRB(18.w, 10.w, 18.w, MediaQuery.of(context).padding.bottom),
              color: Colors.white,
              child: solidButton("新建收货地址", () {
                addressCtr.initAdd();
              }),
            ))
      ],
    );
  }
}

/**
 * 地址项的组件
 */
class _addressItems extends StatefulWidget {

  final AddressListEntity? item;
  final int? index;

  const _addressItems({
    super.key,
    this.item, this.index,
  });

  @override
  State<_addressItems> createState() => __addressItemsState();
}

class __addressItemsState extends State<_addressItems> {
  final AddressController addressCtr = Get.find<AddressController>();


  @override
  Widget build(BuildContext context) {
    return LeftScroll(
      key: Key(widget.item!.id.toString()),
      closeTag: LeftScrollCloseTag(widget.item!.id.toString()),
      child: Container(
      color: Colors.white,
      child: leftBox(),
    ), buttons: [
      deleteBox(widget.item?.id,widget.index!)
    ]);
  }

  Widget deleteBox(dynamic id,int index){
    return Container(
      padding: EdgeInsets.only(top: 5.w,bottom: 5.w),
      child: LeftScrollItem(
      color: Colors.red,
          text: '删除',
          onTap: () {
            Tools.showModal(content: "确认要删除该地址么?" ,onCancel:(tag) {
              
            },onConfirm: (tag) {
              addressCtr.deleteAddress(id,index);
              SmartDialog.dismiss(tag: tag);
            },);
          },
        ),
    );
  }

  Widget leftBox(){
    return Container(
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: const Color.fromRGBO(243, 245, 247, 1),
              width: 0.7.w,
            ),
          ),
        ),
        //两行三列
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            BaseInteractive(
              childWidget: SizedBox(
              width: 280.w,
              height: 100.w,
              // color: Colors.red,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.item!.provinceName.toString() +
                        widget.item!.cityName.toString() +
                        widget.item!.districtName.toString(),
                    style: const TextStyle(
                        fontSize: 12,
                        fontWeight: FontWeight.w300,
                        color: Color.fromRGBO(153, 153, 153, 1)),
                  ),
                  Text(
                    widget.item!.receiveAddress.toString(),
                    style: const TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: Colors.black),
                  ),
                  Row(
                    children: [
                      Text(
                        widget.item!.receiveName.toString(),
                        style: const TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: Color.fromRGBO(153, 153, 153, 1)),
                      ),
                      SizedBox(
                        width: 20.w,
                      ),
                      Text(
                        Tools.encryptPhoneNumber(widget.item!.receivePhone.toString()),
                        style: const TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: Color.fromRGBO(153, 153, 153, 1)),
                      ),
                      SizedBox(
                        width: 10.w,
                      ),
                      Visibility(
                          visible: widget.item?.isDefault == 1 ? true : false,
                          child: Container(
                            decoration: border_radis(Tools.fromHex("#F2260A"),radiusValue: 5.w),
                            padding: EdgeInsets.fromLTRB(4.w, 0.w, 4.w, 2.w),
                            margin: EdgeInsets.only(left: 10.w),
                            child: const Text(
                              "默认",
                              style: TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.w300,
                                  color: Colors.white),
                            ),
                          )),
                    ],
                  )
                ],
              ),
            ),
            onClick:() {
              addressCtr.clickAddressItem(widget.item!);
            },
            ),
            InkWell(
                onTap: () => {
                  addressCtr.updateInfo(widget.item!)
                },
                child: Container(
                  // height: 50,
                  child: BaseImage(url: "address_edit.png", width: 14.w,height: 14.w),
                ))
          ],
        ));
  }
}
